<?php 
if( !isset( $subs)  )
    $subs = Subscription::find_by_user_id($user->user_id);
    
    
    //view($subs);
?>
<td class="align-top">
<section  class="cell-light">
<header>
<h2>Active Subscriptions:</h2>
<?php $session->display_messages('subscription'); ?>
</header>
    <table class="inset-table">
     <thead>
        <tr class="skin-gradient heading-row">
           <th>ID</th>
           <th>Publication</th>
           <th>Start Date</th>
           <th>Current Status</th>
           <th>Duration</th>
           <th>Expires</th>
        </tr>
    </thead>
    <tbody>
        <?php
        foreach ($subs as $sub ) : 
        if ( !$sub->is_active() ) : 
        
        ?>
        <tr>
            <td><?php $sub->attr('subscription_id'); ?></td>
            <td><?php $sub->publication->attr('name'); ?></td>
            <td><?php echo date("F j Y", get_date_from_weeknum( $sub->get_attr('start_week') )); ?> <small>(Week #<?php $sub->attr('start_week'); ?>)</small></td>
            <td><?php echo $sub->get_status(); ?></td>
            <td><?php echo "<strong>Expired</strong>"; ?></td>
            <td><?php echo date("F j Y", strtotime($sub->get_end_date())); ?></td>
        </tr>    
        <?php else: ?>
        <tr>
            <td><?php $sub->attr('subscription_id'); ?></td>
            <td><?php $sub->publication->attr('name'); ?></td>
            <td><?php echo date("F j Y", get_date_from_weeknum( $sub->get_attr('start_week') )); ?> <small>(Week #<?php $sub->attr('start_week'); ?>)</small></td>
            <td><?php echo $sub->get_status(); ?></td>
            <td><strong><?php $sub->current_week(); echo " of " . $sub->subscription_length() . "</strong>"; ?></td>
            <td><?php echo date("F j Y", strtotime($sub->get_end_date())); ?></td>
        </tr>
        <tr>
            <td colspan="6">
            <table  class="inset-table">
                 <thead>
                    <tr class="dark-heading-row">
                       <th>ID</th><th>Allowance Type</th><th>Start</th><th>End</th><th>Duration</th><th>Controls</th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    $bank = $sub->get_attr('start_week');
                    foreach($sub->allowances as $allowance) : 
                    //view($allowance);
                    ?>
                    <tr <?php echo ( $allowance->get_attr('current_duration') < $allowance->get_attr( 'total_duration' )) ? 'class="active"' : ''; ?>>
                        <td><?php $allowance->attr('allowance_id'); ?></td>
                        <td><?php $allowance->attr('type'); ?></td>
                        <td><?php 
                            echo date("F j Y", get_date_from_weeknum($bank));
                            $bank += $allowance->get_attr('total_duration');
                        ?></td>
                        <td><?php echo date("F j Y", get_date_from_weeknum($bank-1)); ?></td>
                        <td><?php $allowance->attr('current_duration'); ?> of <?php  $allowance->attr('total_duration'); ?></td>
                        <td class="width-250 text-right">
                            <a class="show-tool-tip toggle-single-allowance-controls"  title="Edit this allowance." href="#"> edit </a>
                            
                            <div class="single-allowance-controls">
                                <form class="group" method="post" action="<?php echo $Controller->page_url(); ?>&user=<?php $sub->attr('user_id');  ?>">
                                    <input type="number" name="single_allowance_adjustment_amount" placeholder="Weeks" />
                                    <input type="hidden" name="action" value="single_allowance_adjustment"/>
                                    <input type="hidden" name="allowance_id" value="<?php $allowance->attr('allowance_id'); ?>"/>
                                    <input type="hidden" name="subscription_id" value="<?php $sub->attr('subscription_id'); ?>"/>
                                    <hr>
                                    <input class="primary-button skin-gradient right" type="submit" value="Add" name="new_allowance" title="Additional time will be added to this subscription."/>
                                    <a class="show-tool-tip text-button right"  title="This will <em>PERMANENTLY</em> remove this allowance. It is recommended that you only use this feature to correct an allowance that was created by mistake. Try removing weeks instead." href="<?php echo $Controller->page_url('edit'); ?>&user=<?php $user->attr('user_id'); ?>&delete=allowance&id=<?php $allowance->attr('allowance_id'); ?>"> delete </a>
                                </form>
                            </div>
                            
                        </td>
                    </tr>
                    <?php
                    endforeach; ?>
                    <tr>
                        
                        
                        
                        
                        <td colspan="7">
                            <?php
                            /*
                             * 
                             * 
                             *  START ALLOWANCE CONTROLS
                             * 
                             * 
                             */
                            ?>
                            <div id="user-add-allowance-<?php $allowance->attr('allowance_id'); ?>" class="group user-add-allowance">
                                <ul class="left">
                                    <li class="active"> <a id="add-toggle" href="#add-time" title="Add more time...">Add Time</a></li>
                                    <li>                <a id="remove-toggle" href="#remove-time" title="Remove time">Remove Time</a></li>
                                    <li>                <a id="pause-toggle" href="#pause-time" title="Add a pause">Add Pause</a></li>
                                    <li>                <a id="end-toggle" href="#end-time" title="Set end date">Set End Date</a></li>
                                </ul>
                                
                                <div id="user-add-allowance-frame" class="group left width-400">
                                <?php
                                /**
                                 * 
                                 *  ADD TIME frame
                                 * 
                                 */
                                ?>
                                <form id="add-time" class="group" method="post" action="<?php echo $Controller->page_url(); ?>&user=<?php $sub->attr('user_id');  ?>">
                                    <label><input type="radio" name="total_duration" value="156" /><span>3 Years</span></label>
                                    <label><input type="radio" name="total_duration" value="104" /><span>2 Years</span></label>
                                    <label><input type="radio" name="total_duration" value="52" /><span>1 Year</span></label>
                                    <br>
                                    <label>
                                    <input type="radio" name="total_duration" value="0" /><span>Other</span>
                                    <input type="number" name="total_duration_other" placeholder="Weeks" />
                                    </label>
                                    <hr>
                                    <input type="hidden" name="type" value="add"/>
                                    <input type="hidden" name="action" value="new_allowance"/>
                                    <input type="hidden" name="subscription_id" value="<?php $sub->attr('subscription_id'); ?>"/>
                                    <input class="primary-button skin-gradient right" type="submit" value="Add" name="new_allowance" title="Additional time will be added to this subscription."/>
                                    <a class="show-tool-tip right text-button" href="#" title="Select the amount of time you wish to <em>ADD</em> to the current subscription.">Help</a>
                
                                </form>
                                <?php
                                /**
                                 * 
                                 *  REMOVE TIME frame
                                 * 
                                 */
                                ?>
                                <form class="group" id="remove-time" method="post" action="<?php echo $Controller->page_url(); ?>&user=<?php $sub->attr('user_id');  ?>">
                
                                    <label>
                                    <span>Weeks</span>
                                    <input type="number" name="total_duration" placeholder="Weeks" />
                                    </label>
                                    <hr>
                                    <input type="hidden" name="type" value="remove"/>
                                    <input type="hidden" name="action" value="new_allowance"/>
                                    <input type="hidden" name="subscription_id" value="<?php $sub->attr('subscription_id'); ?>"/>
                                    <input class="primary-button skin-gradient right" type="submit" value="Remove" name="new_allowance" title="Time will be subtracted from this subscription."/>
                                    <a class="show-tool-tip right text-button" href="#" title="If you need to remove weeks from a subscription, indicate the amount and hit Remove. <br> <em>Please note: You cannot remove more weeks than are available.</em>">Help</a>
                                </form>
                                <?php
                                /**
                                 * 
                                 *  ADD PAUSE frame
                                 * 
                                 */
                                ?>
                                <form class="group" id="pause-time" method="post" action="<?php echo $Controller->page_url(); ?>&user=<?php $sub->attr('user_id'); ?>">
                                    <label><span>Start date:</span>
                                        <input id="pause-datepicker" class="datepicker" type="text" value="" name="start_date" readonly data-maxdate="<?php echo ( $sub->subscription_length - $sub->get_current_week()); ?>"/>
                                    </label>

                                    <label>
                                    <input type="number" name="total_duration" placeholder="Weeks" />
                                    </label>

                                    <hr>
                                    <input type="hidden" name="subscription_id" value="<?php $sub->attr('subscription_id');  ?>"/>
                                    <input type="hidden" name="action" value="new_allowance"/>
                                    <input type="hidden" name="type" value="pause" />
                                    <input class="primary-button skin-gradient right" type="submit" value="Pause" name="new_allowance" title="A pause will be added to this subscription at the given date, for the given duration."/>
                                    <a class="show-tool-tip right text-button" href="#" title="Schedule a <em>PAUSE</em> in the current subscription. Just indicate the starting week, and the duration (in weeks). <br><em>Please note: You can only schedule pauses for now and in the future.</em>">Help</a>
                                </form>
                                <?php
                                /**
                                 * 
                                 *  SET END DATE frame
                                 * 
                                 */
                                ?>
                                <form class="group" id="end-time" method="post" action="<?php echo $Controller->page_url(); ?>&user=<?php $sub->attr('user_id');  ?>">
                                    <label><span>End Date:</span>
                                        <input class="datepicker" type="text" value="" name="end_date" readonly />
                                    </label>
                                    <hr>
                                    <input type="hidden" name="subscription_id" value="<?php $sub->attr('subscription_id');  ?>"/>
                                    <input  type="hidden" name="type" value="set" />
                                    <input type="hidden" name="action" value="new_allowance"/>
                                    <input class="primary-button skin-gradient right" type="submit" value="Set" name="new_allowance" title="A pause will be added to this subscription at the given date, for the given duration."/>
                                    <a class="show-tool-tip right text-button" href="#" title="Set an explicit <em>END DATE</em> for the current subscription, the allowance will auto-calculate.">Help</a>
                                </form>
                                
                                
                                </div>
                            </div>
                            
                            <a class="toggle-allowance-controls" href="#user-add-allowance-<?php $allowance->attr('allowance_id'); ?>"> Add New Allowance </a>
                        </td>
                    </tr>
                </tbody>
            </table>

            </td>
        </tr>
        <?php endif; endforeach; // end subscription loop ?>
    </tbody>
</table>


</section>

<div id="subscription-buttons" class="group button-group padding margin-top">
<a class="grey button left" href="<?php echo $Controller->page_url('subscriptions'); ?>">&laquo; Back to Subscriptions</a>
<a class="grey button left" href="<?php echo $Controller->page_url('edit'); ?>">Add another Subscription &raquo;</a>
</div>

</td>